package cn.jolyoulu.cmnservice.serivce.impl;


import cn.jolyoulu.common.redis.utils.RedisUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.jolyoulu.cmnservice.constant.ServiceRedisConstant;
import cn.jolyoulu.cmnservice.dao.SysUserMapper;
import cn.jolyoulu.cmnservice.entity.po.SysUser;
import cn.jolyoulu.cmnservice.serivce.SysUserService;

import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisUtils redisUtil;

    @Override
    public SysUser getByIdIgnDelFlag(String id) {
        return sysUserMapper.getByIdIgnDelFlag(id);
    }

    @Override
    public SysUser getByAccount(String account) {
        LambdaQueryWrapper<SysUser> ew = new LambdaQueryWrapper<>();
        ew.eq(SysUser::getAccount,account);
        return sysUserMapper.selectOne(ew);
    }

    @Override
    @Transactional
    public boolean allotRole(String userId, List<String> roleIds) {
        sysUserMapper.deleteUserRoleByUserId(userId);
        sysUserMapper.insertBatchUserRole(userId,roleIds);
        String roleKey = ServiceRedisConstant.getUserRole(userId);
        if (redisUtil.exists(roleKey)){
            redisUtil.del(roleKey);
        }
        return true;
    }
}